<template>
  <van-nav-bar
    :title="$route.meta.title"
    v-show="$route.meta.isShowNav"
    @click-left="onClickLeft"
    :left-arrow="$route.meta.isShowBack"
    fixed
    placeholder
    style="height: 46px"
  />
  <router-view></router-view>
  <tab-bar v-if="isShowTabbar"></tab-bar>
</template>

<script setup>
import TabBar from './components/TabBar.vue'
import { useRoute, useRouter } from 'vue-router'
import { ref, watch, onMounted } from 'vue'
import { getUser } from './api'
import useUser from './stores/user'

const route = useRoute()
const isShowTabbar = ref(true)
const router = useRouter()
const { user, updateUser } = useUser()

const onClickLeft = () => {
  if (route.name === 'cart') {
    router.push({ name: 'home' })
  } else if (router.currentRoute.value.matched.length == 1) {
    router.back()
  } else {
    router.push({ name: 'home' })
  }
}


onMounted(() => {
  if (user.isLogin) {
    loadUser()
  }
})

const loadUser = async () => {
  const data = await getUser()
  updateUser({
    isLogin: true,
    username: data.username,
    avatar: data.avatar
  })
}

// 监听路由中的 isTab 是否为 true，如果为 true，展示底部 TabBar
watch(
  () => route.meta,
  val => {
    isShowTabbar.value = val.isTab
  }
)
</script>

<style>
#app {
  color: #2c3e50;
  line-height: 24px;
  --van-nav-bar-background: #f3f3f3;
  --van-nav-bar-title-text-color: #000000;
  --van-nav-bar-icon-color: #020202;
}
</style>